package com.test.singleton.test1;

/**
 * @Author: junwe
 * @Date: 2020/12/6 16:25
 */
public class SingletonTest1 {
    public static void main(String[] args) {
        Singleton instance1 = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance1 == instance2);
        System.out.println(instance1.hashCode() == instance2.hashCode());
    }

}

/**
 * 饿汉式-静态常量
 */
class Singleton {

    /**
     * 私有化构造器，外部类不能new对象
     */
    private Singleton() {}

    // 类被加载的时候创建对象实例
    private final static Singleton INSTANCE = new Singleton();

    /**
     * 提供一个公有的静态方法返回对象实例
     * @return
     */
    public static Singleton getInstance() {
        return INSTANCE;
    }
}
